﻿/////////////////////////////////////////////////////////////////////////////////
//
// InstantCube
// - Automatically generates Analysis Services cubes
// (C) Copyright Johan Åhlén 2009
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//
// More info on InstantCube website: http://instantcube.codeplex.com
//
/////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

namespace AdvantIQ.InstantCube.Model
{
    public class Attribute
    {
        public string ID { get; set; }
        public AttributePart[] Parts { get; set; }
        public bool IsParentRef { get; set; }
        public bool Visible { get; set; }
        public List<string> RelatedRigidAttributes { get; set; }
        public List<string> RelatedFlexibleAttributes { get; set; }

        public AttributePart Key
        {
            get
            {
                return Parts.Where(a => a.PartType == AttributePart.AttributePartType.Key).DefaultIfEmpty(null).Single();
            }
        }

        public AttributePart Name
        {
            get
            {
                return Parts.Where(a => a.PartType == AttributePart.AttributePartType.Name).DefaultIfEmpty(null).Single();
            }
        }

        public AttributePart Value
        {
            get
            {
                return Parts.Where(a => a.PartType == AttributePart.AttributePartType.Value).DefaultIfEmpty(null).Single();
            }
        }

        public Attribute(XElement srcNode, Project project)
        {
            try
            {
                RelatedRigidAttributes = new List<string>();
                RelatedFlexibleAttributes = new List<string>();

                if (srcNode == null)
                    return;

                ID = srcNode.Attribute("id").Value;

                if (string.Compare(srcNode.Attribute("type").SafeValue(), "Parent", true) == 0)
                {
                    Parts = new[]
                                {
                                    new AttributePart(AttributePart.AttributePartType.Key, project.DefaultTypes.SurrogateKey, ID, project)
                                        {
                                            FieldName = ID
                                        }
                                };
                    IsParentRef = true;
                    return;
                }

                var lst = new List<AttributePart>();
                foreach (var node in srcNode.Elements())
                {
                    lst.Add(new AttributePart(node, ID, project));
                }
                Parts = lst.ToArray();
            }
            catch (ModelErrorException ex)
            {
                throw new ModelErrorException(this, ex);
            }
            catch (Exception ex)
            {
                throw new ModelErrorException(this, ex.Message);
            }
        }

        public override string ToString()
        {
            if (ID != null)
                return string.Format("Attribute({0})", ID);
            else
                return "Attribute";
        }
    }
}
